/**
 * @file motorf.c
 * @author      JinChengLong (https://gitee.com/Datejin/)
 * @brief       电机2库函数
 * @version     0.1
 * @date        2023
 * 
 * @copyright   Copyright © 2025 by JinChengLong 
 * 
 * All rights reserved.
 * Unauthorized copying, reproduction, or redistribution of this file via any medium is strictly prohibited.  
 * Proprietary and confidential.
 *   
 */


#include "motorf.h"
#include "motor.h"

void pwmW_init(void)
{

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);			//初始化PA0引脚
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);			//打开TIM2定时器时钟
	TIM_InternalClockConfig(TIM4);		//选择为72M内部时钟（默认内部时钟）
	
	
	
	//初始化TIM2通道
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7 ;					//电机接到通道三上//PA2
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;			//复用上拉输入用定时器控制引脚
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;					//电机接到通道三上//PA2
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;			//复用上拉输入用定时器控制引脚
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	//时基单元配置
	//定时频率=72M/（psc+1）/（ARR+1）
	//10k=10ms
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision =TIM_CKD_DIV1;		//TIM2 1分频
	TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up;	//向上计数(从零到重装值）
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter =0;				//重复计数器的值（高级定时器）
	TIM_TimeBaseInitStructure.TIM_Prescaler	=720-1;						//PSC预分频器的值(0到65535）72M频率变为10kHz
	TIM_TimeBaseInitStructure.TIM_Period =100-1	;						//ARR自动重装器的值（周期）（0到65535）
																			//计算一秒的时间10k的频率计算10000个数，一秒一个周期
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);
									
	//输出比较单元
	TIM_OCInitTypeDef TIM_OCInitSturcture;
	TIM_OCStructInit(&TIM_OCInitSturcture);		 			//结构体没有完全设置，为剩余部分赋予初始值
	TIM_OCInitSturcture.TIM_OCMode=TIM_OCMode_PWM1;			//设置为PWM模式一
	TIM_OCInitSturcture.TIM_OCPolarity=TIM_OCPolarity_High;	//	高极性，极性不翻转，REF直接输出，高电平有效
	TIM_OCInitSturcture.TIM_OutputState=TIM_OutputState_Enable;	//输出使能
	TIM_OCInitSturcture.TIM_Pulse=	0;						//CCR的值
	
	TIM_OC2Init(TIM4, &TIM_OCInitSturcture);//只能在TIM2CH2借用的PA1引脚
	
	TIM_OC1Init(TIM4, &TIM_OCInitSturcture);//只能在TIM2CH2借用的PA1引脚
	
	//  6，使能ARR的预装载寄存器
    TIM_ARRPreloadConfig(TIM4,ENABLE);

    // 7，使能通道1，2，3的预装载寄存器
    TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
 

	//启动定时器
	TIM_Cmd(TIM4,ENABLE);									//定时器正常工作
	
}

/**
 * @brief 电机2左轮
 * 
 * @param compare 速度0~100
 */
void PWMzf_control(uint16_t compare)
{
	TIM_SetCompare1(TIM4,compare);			//是设置CCR寄存器的值不直接是占空比，由公式得到的

}	
/**
 * @brief 电机2右轮
 * 
 * @param compare 速度0~100
 */
void PWMyf_control(uint16_t compare)
{
	TIM_SetCompare2(TIM4,compare);			//是设置CCR寄存器的值不直接是占空比，由公式得到的

}	




/**
 * @brief 电机2初始化
 * 
 */
void motorf_init(void)
{
	pwmW_init();
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);			//初始化PA0引脚
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;					//电机接到通道三上//PA2
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;			//
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_4;					//电机接到通道三上//PA2
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;			//
	GPIO_Init(GPIOA,&GPIO_InitStructure);
}



/**
 * @brief 设置电机速度
 * 
 * @param speedzf 电机2左轮
 * @param speedyf 电机2右轮
 */
void motorf_speed(int8_t speedzf,int8_t speedyf)
{
	motorf_init();
	if(speedzf>=0)
	{
		GPIO_SetBits(GPIOB,GPIO_Pin_1);
		GPIO_ResetBits(GPIOB,GPIO_Pin_0);
		PWMzf_control(speedzf);
	
	}
	else if(speedzf<0)
	{
		GPIO_SetBits(GPIOB,GPIO_Pin_0);
		GPIO_ResetBits(GPIOB,GPIO_Pin_1);
		PWMzf_control(-speedzf);
	}
	if(speedyf>=0)
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_4);
		GPIO_ResetBits(GPIOA,GPIO_Pin_3);
		PWMyf_control(speedyf);
	}
	else if(speedyf<0)
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_3);
		GPIO_ResetBits(GPIOA,GPIO_Pin_4);
		PWMyf_control(-speedyf);
	}
}

